﻿//
// (C) Copyright 2009 Irantha Suwandarathna (iranthas@hotmail.com)
// All rights reserved.
//

/* Copyright (c) 2001-2008, The HSQL Development Group
 * All rights reserved.
 *
 * Redistribution and use _in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions _in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer _in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the HSQL Development Group nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EffiProz.Core.Lib
{
    public class EfzDeque : BaseList, EfzList
    {
        private Object[] list;
        private int firstindex = 0;    // index of first list element
        private int endindex = 0;    // index of last list element + 1

        // can grow to fill list
        // if elementCount == 0 then firstindex == endindex
        private const int DEFAULT_INITIAL_CAPACITY = 10;

        public EfzDeque()
        {
            list = new Object[DEFAULT_INITIAL_CAPACITY];
        }

        public override int size()
        {
            return elementCount;
        }

        public override bool isEmpty()
        {
            return elementCount == 0;
        }

        public Object getFirst()
        {

            if (elementCount == 0)
            {
                throw new KeyNotFoundException();
            }

            return list[firstindex];
        }

        public Object getLast()
        {

            if (elementCount == 0)
            {
                throw new KeyNotFoundException();
            }

            return list[endindex - 1];
        }

        public override Object get(int i)
        {

            int index = getInternalIndex(i);

            return list[index];
        }

        public void add(int i, Object o)
        {
            throw new NotSupportedException();
        }

        public Object set(int i, Object o)
        {

            int index = getInternalIndex(i);
            Object result = list[index];

            list[index] = o;

            return result;
        }

        public Object removeFirst()
        {

            if (elementCount == 0)
            {
                throw new KeyNotFoundException();
            }

            Object o = list[firstindex];

            list[firstindex] = null;

            firstindex++;
            elementCount--;

            if (elementCount == 0)
            {
                firstindex = endindex = 0;
            }
            else if (firstindex == list.Length)
            {
                firstindex = 0;
            }

            return o;
        }

        public Object removeLast()
        {

            if (elementCount == 0)
            {
                throw new KeyNotFoundException();
            }

            endindex--;

            Object o = list[endindex];

            list[endindex] = null;

            elementCount--;

            if (elementCount == 0)
            {
                firstindex = endindex = 0;
            }
            else if (endindex == 0)
            {
                endindex = list.Length;
            }

            return o;
        }

        /*
            public Object remove(int i){
                return get(i);
            }

            public void add(int i, Object o) {

            }
        */
        public override bool add(Object o)
        {

            resetCapacity();

            if (endindex == list.Length)
            {
                endindex = 0;
            }

            list[endindex] = o;

            elementCount++;
            endindex++;

            return true;
        }

        public bool addLast(Object o)
        {
            return add(o);
        }

        public bool addFirst(Object o)
        {

            resetCapacity();

            firstindex--;

            if (firstindex < 0)
            {

                if (endindex == 0)
                {
                    endindex = list.Length;
                }
            }

            list[firstindex] = o;

            elementCount++;

            return true;
        }

        public void clear()
        {

            if (elementCount == 0)
            {
                return;
            }

            firstindex = endindex = elementCount = 0;

            for (int i = 0; i < list.Length; i++)
            {
                list[i] = null;
            }
        }

        public override int indexOf(Object value)
        {

            for (int i = 0; i < elementCount; i++)
            {
                int index = firstindex + i;

                if (index >= list.Length)
                {
                    index -= list.Length;
                }

                if (list[index] == value)
                {
                    return i;
                }

                if (value != null && value.Equals(list[index]))
                {
                    return i;
                }
            }

            return -1;
        }

        public override Object remove(int index)
        {

            int target = getInternalIndex(index);
            Object value = list[target];

            if (target == firstindex)
            {
                list[firstindex] = null;

                firstindex++;

                if (firstindex == list.Length)
                {
                    firstindex = 0;
                }
            }
            else if (target > firstindex)
            {
                Array.Copy(list, firstindex, list, firstindex + 1,
                                 target - firstindex);

                list[firstindex] = null;

                firstindex++;

                if (firstindex == list.Length)
                {
                    firstindex = 0;
                }
            }
            else
            {
                Array.Copy(list, target + 1, list, target,
                                 endindex - target - 1);

                endindex--;

                list[endindex] = null;

                if (endindex == 0)
                {
                    endindex = list.Length;
                }
            }

            elementCount--;

            if (elementCount == 0)
            {
                firstindex = endindex = 0;
            }

            return value;
        }

        private int getInternalIndex(int i)
        {

            if (i < 0 || i >= elementCount)
            {
                throw new IndexOutOfRangeException();
            }

            int index = firstindex + i;

            if (index >= list.Length)
            {
                index -= list.Length;
            }

            return index;
        }

        private void resetCapacity()
        {

            if (elementCount < list.Length)
            {
                return;
            }

            Object[] newList = new Object[list.Length * 2];

            Array.Copy(list, firstindex, newList, firstindex,
                             list.Length - firstindex);

            if (endindex <= firstindex)
            {
                Array.Copy(list, 0, newList, list.Length, endindex);

                endindex = list.Length + endindex;
            }

            list = newList;
        }

        public void toArray(Object[] array)
        {

            int tempCount = list.Length - firstindex;

            if (tempCount > elementCount)
            {
                tempCount = elementCount;
            }

            Array.Copy(list, firstindex, array, 0, tempCount);

            if (endindex <= firstindex)
            {
                Array.Copy(list, 0, array, tempCount, endindex);

                endindex = list.Length + endindex;
            }
        }
    }
}
